Skip to content

HTTP GET请求 - HttpGet

函数简介

发送简单的HTTP GET请求,返回响应体字符串。

接口名称

HttpGet

DLL调用

c
const char* HttpGet(int64_t instance, const char* url);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
url字符串完整URL

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// GET 请求获取响应体字符串
string body = ola.HttpGet("https://httpbin.org/get");
// body 为响应正文,失败时可能为空
csharp
using OLAPlug;

var ola = new OLAPlugServer();
// GET 请求获取响应体字符串
string body = ola.HttpGet("https://httpbin.org/get");
// body 为响应正文,失败时可能为空
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
# GET 请求获取响应体字符串
body = ola.HttpGet("https://httpbin.org/get")
# body 为响应正文,失败时可能为空
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
// GET 请求获取响应体字符串
String body = ola.HttpGet("https://httpbin.org/get");
// body 为响应正文,失败时可能为空
cpp
var ola = com("OlaPlug.OlaSoft")
// GET 请求获取响应体字符串
var body = ola.HttpGet("https://httpbin.org/get")
// body 为响应正文,失败时可能为空
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
' GET 请求获取响应体字符串
body = ola.HttpGet("https://httpbin.org/get")
' body 为响应正文,失败时可能为空
text
.局部变量 ola, OLAPlug
ola.创建 ()
' GET 请求获取响应体字符串
body = ola.HttpGet(“https://httpbin.org/get“)
' body 为响应正文,失败时可能为空
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
// GET 请求获取响应体字符串
var body = ola.HttpGet("https://httpbin.org/get");
// body 为响应正文,失败时可能为空
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
// GET 请求获取响应体字符串
文本型 body = ola.HttpGet("https://httpbin.org/get")
// body 为响应正文,失败时可能为空
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// GET 请求获取响应体字符串
string body = ola.HttpGet("https://httpbin.org/get");
// body 为响应正文,失败时可能为空

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long bodyPtr = HttpGet(instance, "https://httpbin.org/get");
if (bodyPtr != 0) {
    char body[512] = {0};
    GetStringFromPtr(bodyPtr, body, sizeof(body));
    FreeStringPtr(bodyPtr);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringFromPtr(long ptr, StringBuilder lpString, int size);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int FreeStringPtr(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int GetStringSize(long ptr);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();

long instance = CreateCOLAPlugInterFace();
long bodyPtr = HttpGet(instance, "https://httpbin.org/get");
if (bodyPtr != 0) {
    StringBuilder body = new StringBuilder(GetStringSize(bodyPtr) + 1);
    GetStringFromPtr(bodyPtr, body, body.Capacity);
    FreeStringPtr(bodyPtr);
    string bodyStr = body.ToString();
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
bodyPtr = HttpGet(instance, "https://httpbin.org/get")
if bodyPtr:
    buf = create_string_buffer(512)
    ola.GetStringFromPtr(bodyPtr, buf, 512)
    ola.FreeStringPtr(bodyPtr)
    body = buf.value.decode("utf-8")

返回值

字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

  • 返回的字符串需要调用 FreeStringPtr 释放内存
  • 适合简单的GET请求
  • 如需自定义请求头或Cookie,请使用 HttpRequestEx
  • 支持HTTP和HTTPS协议